//
//  SqliteHelp.m
//  park-mobile
//
//  Created by Goblin on 2017/10/12.
//

#import "SqliteHelp.h"
#import "NSString+safeAssignment.h"


@implementation SqliteHelp


+(void)saveDownloadWith:(NSString *)audio_id{
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    NSString *idd = [NSString stringWithFormat:@"gcgcdownloadid_%@",audio_id];
    [defs setInteger:1 forKey:idd];
    [defs synchronize];
}

+(BOOL)isSaveDownloadWithid:(NSString *)audio_id{
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    NSString *idd = [NSString stringWithFormat:@"gcgcdownloadid_%@",audio_id];
    if ([defs integerForKey:idd] == 1) {
        return YES;
    }
    return NO;
}

+(void)deleteDownloadWithid:(NSString *)audio_id{
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    NSString *idd = [NSString stringWithFormat:@"gcgcdownloadid_%@",audio_id];
    [defs setInteger:0 forKey:idd];
    [defs synchronize];
    NSDictionary* defaults = [defs dictionaryRepresentation];
    for (NSString *key in defaults) {
        if ([key isEqualToString:idd]) {
            [defs removeObjectForKey:key];
            [defs synchronize];
            return;
        }
    }
}

+(void)deleteDownload{
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    NSDictionary* defaults = [defs dictionaryRepresentation];
    for (NSString * key in defaults) {
        if ([key hasPrefix:@"gcgcdownloadid"]) {
            [defs removeObjectForKey:key];
            [defs synchronize];
            return;
        }
    }
}


+(void)deleteAudiodic{
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    NSDictionary* defaults = [defs dictionaryRepresentation];
    for (NSString * key in defaults) {
        if ([key hasPrefix:@"gcgcwithid"]) {
            [defs removeObjectForKey:key];
            [defs synchronize];
            return;
        }
    }
}

+(NSDictionary *)takeAudioDicWithid:(NSString *)audio_id{
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    NSDictionary* defaults = [defs dictionaryRepresentation];
    NSString *idd = [NSString stringWithFormat:@"gcgcwithid_%@",audio_id];
    for (NSString * key in defaults) {
        if ([key isEqualToString:idd]) {
            return [SqliteHelp dictionaryWithJsonString:[defs objectForKey:idd]];
        }
    }
    return nil;
}

+(void)saveAudioDicWith:(NSDictionary *)dic{
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    NSString *idd = [NSString stringWithFormat:@"gcgcwithid_%@",dic[@"id"]];
    [defs setObject:[SqliteHelp convertToJsonData:dic] forKey:idd];
    [defs synchronize];
}

+(BOOL)isSaveAudiodicWithid:(NSString *)audio_id{
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    NSDictionary* defaults = [defs dictionaryRepresentation];
    NSString *idd = [NSString stringWithFormat:@"gcgcwithid_%@",audio_id];
    for (NSString * key in defaults) {
        if ([key isEqualToString:idd]) {
            return YES;
        }
    }
    return NO;
}

+(void)deleteAudiodicWithid:(NSString *)audio_id{
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    NSDictionary* defaults = [defs dictionaryRepresentation];
    NSString *idd = [NSString stringWithFormat:@"gcgcwithid_%@",audio_id];
    for (NSString * key in defaults) {
        if ([key isEqualToString:idd]) {
            [defs removeObjectForKey:key];
            [defs synchronize];
            return;
        }
    }
}




+(NSString *)convertToJsonData:(NSDictionary *)dict

{

    NSError *error;

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];

    NSString *jsonString;

    if (!jsonData) {

        NSLog(@"%@",error);

    }else{

        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];

    }

    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];

    NSRange range = {0,jsonString.length};

    //去掉字符串中的空格

    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];

    NSRange range2 = {0,mutStr.length};

    //去掉字符串中的换行符

    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];

    return mutStr;

}

+(NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
    if (jsonString == nil) {
        return nil;
    }

    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

@end
